/* graphreader.cpp
Desc.: Graph reader class implementation
Auth.: Ashwin Lall
Start: March 9, 2014
*/ 

#include "graphreader.h"
#include <fstream>
using namespace std;

GraphReader::GraphReader(string filename)
{
    _file.open(filename.c_str());
    _curr = 1;
}

bool GraphReader::nextEdge(int &node1, int &node2)
{
    string line;
    int n1 , n2, curr = 1;
        
    if (!_file.is_open() || _file.eof())
	return false;

    while (getline(_file, line) && line[0] == '#') { }  // loop until end of file or actual edge

    // close file if done
    if (_file.eof())
    {
	_file.close();
	return false;
    }
    
    sscanf(line.c_str(), "%d %d", &n1, &n2);

    // re-lable nodes so that they are 1,2,3,....
    if (_label.count(n1) == 0)
    {
	_label[n1] = _curr;
	_curr++;
    }
    if (_label.count(n2) == 0)
    {
	_label[n2] = _curr;
	_curr++;
    }
    
    node1 = n1;
    node2 = n2;

    return true;
}
